package com.youdelu.road.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youdelu.road.R;
import com.youdelu.road.bean.FileDir;

import java.util.ArrayList;
import java.util.List;

public class GridAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context context=null;
    private List<FileDir> dirs = null;
    private boolean show_add ;
    private class Holder{
        ImageView item_img;
        TextView item_tex;
        public ImageView getItem_img() {
            return item_img;
        }
        public void setItem_img(ImageView item_img) {
            this.item_img = item_img;
        }
        public TextView getItem_tex() {
            return item_tex;
        }
        public void setItem_tex(TextView item_tex) {
            this.item_tex = item_tex;
        }
    }
    //构造方法
    public GridAdapter(Context context, List<FileDir> dirs,boolean show_add) {
        this.show_add = show_add ;
        if(dirs==null){
            dirs = new ArrayList<>();
        }
        this.inflater= LayoutInflater.from(context);
        this.context = context;
        this.dirs =  fitter(dirs);
    }

    public List<FileDir> fitter(List<FileDir> dirs){
        if(show_add) {
            FileDir fileDir = new FileDir();
            fileDir.setName("增加类别");
            fileDir.setType(2);
            dirs.add(fileDir);
        }
        return  dirs;
    }

    public void update(List<FileDir> dirs){
        this.dirs = fitter(dirs);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dirs.size();

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        Holder holder;
        if(view==null){
            view= inflater.inflate(R.layout.view_gridview_item,null);
            holder=new Holder();
            holder.item_img= view.findViewById(R.id.item_img);
            holder.item_tex= view.findViewById(R.id.item_text);
            view.setTag(R.id.tag,holder);
        }else{
            holder=(Holder) view.getTag(R.id.tag);
        }

        view.setTag(R.id.tag_first,dirs.get(position));
        holder.item_tex.setText(dirs.get(position).getName());
        String dir = dirs.get(position).getDir();
        int type = dirs.get(position).getType();
        if(dir == null ||"".equals(dir)){
            if(type==0){
                holder.item_img.setImageResource(R.drawable.file);
            }else if(type==1){
                holder.item_img.setImageResource(R.drawable.photo);
            }else if(type==2){
                holder.item_img.setImageResource(R.drawable.add_dir);
            }
        }else{
            Glide.with(context).load(dir).into(holder.item_img);
        }
        return view;
    }
}